package com.yoyo.svgdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.yoyo.svgdemo.R;

import java.util.Collections;
import java.util.List;

/**
 * Created by Missa on 15/8/31.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context context;
    private LayoutInflater inflater;
    private List<String> data = Collections.emptyList();
    private OnRecyclerViewItemClickListener mRecyclerItemClickLister;


    public RecyclerAdapter(Context context, List<String> data){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycler_item, parent, false);
        ItemHolder holder = new ItemHolder(view);

//        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ItemHolder holder = (ItemHolder) viewHolder;
        holder.button.setText(data.get(position));
        holder.button.setTag(position);
        holder.button.setOnClickListener(this);
//        holder.button.setOnClickListener();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnRecyclerViewClickListener(OnRecyclerViewItemClickListener listener){
        this.mRecyclerItemClickLister = listener;
    }

    @Override
    public void onClick(View view) {
        if(mRecyclerItemClickLister != null){
            mRecyclerItemClickLister.onItemClick(view, (int)view.getTag());
        }
    }

    public interface OnRecyclerViewItemClickListener {
        public void onItemClick(View view, int position);
    }

    class ItemHolder extends RecyclerView.ViewHolder{
        private Button button;

        public ItemHolder(View itemView) {
            super(itemView);

            button = ((Button) itemView.findViewById(R.id.item_tv));

//            itemView.setOnClickListener();
        }

    }
}
